
#include "signup.h"
#include "ui_signup.h"
#include "widget.h"
#include "QMovie"

signup::signup(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::signup)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);//取消窗口标题
    QMovie *movie = new QMovie(":/picture/signup.gif");
    movie->start();
    ui->label_background->setMovie(movie);

    // 确保动画适应标签大小
    movie->setScaledSize(ui->label_background->size());
}

signup::~signup()
{
    delete ui;
}

void signup::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    // 调整 label_background 的大小以匹配窗口大小
    ui->label_background->setGeometry(rect());
}

void signup::on_btn_sure_clicked()
{
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_passwd->text();
    QString surepass = ui->lineEdit_surepasswd->text();

    // 检查输入是否为空
       if (username.isEmpty() || password.isEmpty() || surepass.isEmpty()) {
           QMessageBox::information(this, "注册认证", "所有字段不能为空！");
           return;
       }

    //判断两次密码是否正确
    if(password == surepass){
        //创建数据库执行对象
        QSqlQuery query;
        QString sql = QString("insert into admin (username,password) values('%1','%2');").arg(username).arg(password);

        // 检查用户名是否已存在
                QString checkSql = QString("SELECT COUNT(*) FROM admin WHERE username='%1';").arg(username);
                if (!query.exec(checkSql)) {
                    QMessageBox::critical(this, "错误", "无法执行查询: " + query.lastError().text());
                    return;
                }

                int count = 0;
                if (query.next()) {
                    count = query.value(0).toInt();
                }

                if (count > 0) {
                    QMessageBox::information(this, "注册认证", "该用户名已存在，请选择其他用户名！");
                    return;
                }

        if(!query.exec(sql)){
            QMessageBox::information(this,"注册认证","注册失败，数据插入失败！");

        }else{
            QMessageBox::information(this,"注册认证","注册成功！");
        }
    }else{
        QMessageBox::information(this,"注册认证","两次输入密码不一致！");

    }

}

void signup::on_btn_return_clicked()
{
    Admin *q = new Admin;
    q->show();
    this->close();
}
